

clear all
set more off, perm
capture log close
set maxvar 32767
global klmChinaReach2: env klmChinaReach2
global datapath "C:\Users\hp\Dropbox\ziyan\Pair Point Estimates\Figures\vector model"
cd "C:\Users\hp\Dropbox\ziyan\Pair Point Estimates\Figures\vector model"

**# Cognitive #1
*Using betas of the model WITHOUT scale invariance 
preserve
use "$datapath\estimates\maturation\cog_level_mul\point_estimates_mul_mat_wo.dta" , clear
keep if n>30 & n<38 
sum m2 if n==31
gen teach=r(mean) 
sum m2 if n==32
gen hvc=r(mean) 
sum m2 if n==33
gen hvchild=r(mean)  
sum m2 if n==34
gen grandr=r(mean)  
gen id=1
keep if n ==31
keep id teach hvc hvchild grandr 
tempfile betas
save `betas'
restore 

// notice you can use excel point estimates 

use eta_cog_mul_final_vec, clear
bys KIDID: egen first_a=max(first_agem)
replace eta=. if eta==0
collapse eta (max) first_a first_agem tabm_m intabm_m inttcm_m grandm male eduy_f eduy_m eduy_gm abi_ct (min) i, by (id ts_id)
cap drop _merge
// if you do not use the above method, you can skip the code between line 39-49
merge m:1 id  using `betas'
cap drop temp
foreach var of varlist teach hvc hvchild grandr {
egen temp=max(`var')
replace `var'=temp
drop temp 
}

foreach var of varlist tabm_m intabm_m inttcm_m {
replace `var'=. if `var'==-99
}
// here you can manually general the variables using the point estimates from the excel. Here teach, hvc and hvchild indicates the point estimates of these three variables in the excel file. 
gen teach_exp=exp(0.045*tabm_m) // EXP(teach*tabm_m) 

gen hvc_exp=exp(7.881*intabm_m) // EXP(hvc*intabm_m) 

gen hvchild_exp=exp(0.003*inttcm_m) // EXP(hvchild*inttcm_m)


*Age of enrollment:
gen age_g=1 if first_a>=9 & first_a<15
replace  age_g=2 if first_a>=15 & first_a<=20
replace age_g=3 if first_a>20 & first_a<=25

gen age_g_1_2=(age_g==1 & age_g!=.) //9-15 vs 15-20
replace age_g_1_2=. if age_g==3 | age_g==.
gen age_g_1_3=(age_g==1 & age_g!=.) //9-15 vs 20-25
replace age_g_1_3=. if age_g==2 | age_g==.
gen age_g_2_3=(age_g==2 & age_g!=.) //15-20 vs 20-25
replace age_g_2_3=. if age_g==1 | age_g==.

* Ability 
gen abi_ct_1_2=(abi_ct==1 & abi_ct!=.) //fast-normal
replace abi_ct_1_2=. if abi_ct==3 | abi_ct==.
gen abi_ct_1_3=(abi_ct==1 & abi_ct!=.) //fast -slow
replace abi_ct_1_3=. if abi_ct==2 | abi_ct==.
gen abi_ct_2_3=(abi_ct==2 & abi_ct!=.) //normal-slow
replace abi_ct_2_3=. if abi_ct==1 | abi_ct==.


* Family Background 
gen f_educ=(eduy_f>9 & eduy_f!=.)
gen m_educ=(eduy_m>9 & eduy_m!=.)
gen gm_educ=(eduy_gm>3 & eduy_gm!=.)


* Two group variables

mat define edtest=J(12,4,.)
local m =1 

foreach vary of varlist f_educ m_educ gm_educ   {
local j=1
foreach var of varlist eta teach_exp hvc_exp hvchild_exp{
if "`var'"=="eta"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat edtest[`m',`j']= r(mu_1) // var=0 no educ
	mat edtest[`m'+1,`j']= r(mu_2) //var=1  educ
	mat edtest[`m'+2,`j']=r(p)
	mat edtest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list edtest

*Three group var : ability and age 
mat define abitest=J(12,4,.)
local m =1 

foreach vary of varlist  abi_ct_1_3 abi_ct_1_2 abi_ct_2_3 {
local j=1
foreach var of varlist eta teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat abitest[`m',`j']= r(mu_1) //lower abi
	mat abitest[`m'+1,`j']= r(mu_2) //high abi
	mat abitest[`m'+2,`j']=r(p)
	mat abitest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list abitest


mat define agetest=J(12,4,.)
local m =1 
foreach vary of varlist  age_g_1_3 age_g_1_2  age_g_2_3 {
	local j=1
foreach var of varlist eta teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat agetest[`m',`j']= r(mu_1) // Older Group
	mat agetest[`m'+1,`j']= r(mu_2) //Younger group
	mat agetest[`m'+2,`j']=r(p)
	mat agetest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list agetest




gen n=_n 
gen var=""
gen var2=""



preserve
	svmat edtest 
	rename (edtest1 edtest2 edtest3 edtest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Father's Educ<=9 Mean" if n== 1 
	replace var2="Father's Educ>9 Mean" if n== 2 
	replace var2="Mother's Educ<=9 Mean" if n== 5
	replace var2="Mother's Educ>9 Mean" if n== 6 
	replace var2="Grandmother's Educ<3 Mean" if n== 9 
	replace var2="Grandmother's Educ>=3 Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save edtest_c.dta, replace 
	texsave using "edtest_c.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 



preserve
svmat agetest 
	rename (agetest1 agetest2 agetest3 agetest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)
	replace var2="Age enrollment 20-25 Months Mean" if n== 1 
	replace var2="Age enrollment 9-15 Months Mean" if n== 2 
	replace var2="Age enrollment 15-20 Months Mean" if n== 5
	replace var2="Age enrollment 9-15 Months Mean" if n== 6 
	replace var2="Age enrollment 20-25 Months Mean" if n== 9 
	replace var2="Age enrollment 15-20 Months Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save agetest_c.dta, replace 
	texsave using "$datapath\agetest_c.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 


preserve
	svmat abitest 
	rename (abitest1 abitest2 abitest3 abitest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Slow Mean" if n== 1 
	replace var2="Fast Mean" if n== 2 
	replace var2="Normal Mean" if n== 5
	replace var2="Fast Mean" if n== 6 
	replace var2="Slow Mean" if n== 9 
	replace var2="Normal Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save abitest_c.dta, replace 
	texsave using "$datapath\abitest_c.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 




**# Language#1


/*
preserve
use "$datapath\estimates\maturation\lang_level_mul\point_estimates_mul_mat_wo.dta" , clear
keep if n>25 & n<33
sum m2 if n==26
gen teach=r(mean) 
sum m2 if n==27
gen hvc=r(mean) 
sum m2 if n==28
gen hvchild=r(mean)  
sum m2 if n==29
gen grandr=r(mean)  
gen id=1
keep if n ==26
keep id teach hvc hvchild grandr 
tempfile betas
save `betas'
restore 
*/

// above is to read language point estimates and we can skip it. 

use eta_lang_mul_final_vec, clear
bys KIDID: egen first_a=max(first_agem)
replace eta=. if eta==0
collapse eta (max) first_a first_agem tabm_m intabm_m inttcm_m grandm male eduy_f eduy_m eduy_gm abi_lt (min) i, by (id ts_id)
cap drop _merge
// we can skip line 312 to line 321
merge m:1 id  using `betas'
cap drop temp
foreach var of varlist teach hvc hvchild grandr {
egen temp=max(`var')
replace `var'=temp
drop temp 
}
foreach var of varlist tabm_m intabm_m inttcm_m {
replace `var'=. if `var'==-99
}
// here you can manually general the variables using the point estimates from the excel file (language point estimates). Here teach, hvc and hvchild indicates the point estimates of these three variables in the excel file.

gen teach_exp=exp(0.435*tabm_m) // exp(teach*tabm_m)

gen hvc_exp=exp(4.272*intabm_m) // exp(hvc*intabm_m)

gen hvchild_exp=exp(0.372*inttcm_m) // exp(hvchild*inttcm_m) 


*Age of enrollment:
gen age_g=1 if first_a>=9 & first_a<15
replace  age_g=2 if first_a>=15 & first_a<=20
replace age_g=3 if first_a>20 & first_a<=25

gen age_g_1_2=(age_g==1 & age_g!=.) //9-15 vs 15-20
replace age_g_1_2=. if age_g==3 | age_g==.
gen age_g_1_3=(age_g==1 & age_g!=.) //9-15 vs 20-25
replace age_g_1_3=. if age_g==2 | age_g==.
gen age_g_2_3=(age_g==2 & age_g!=.) //15-20 vs 20-25
replace age_g_2_3=. if age_g==1 | age_g==.

* Ability 
gen abi_lt_1_2=(abi_lt==1 & abi_lt!=.) //fast-normal
replace abi_lt_1_2=. if abi_lt==3 | abi_lt==.
gen abi_lt_1_3=(abi_lt==1 & abi_lt!=.) //fast -slow
replace abi_lt_1_3=. if abi_lt==2 | abi_lt==.
gen abi_lt_2_3=(abi_lt==2 & abi_lt!=.) //normal-slow
replace abi_lt_2_3=. if abi_lt==1 | abi_lt==.


* Family Background 
gen f_educ=(eduy_f>9 & eduy_f!=.)
gen m_educ=(eduy_m>9 & eduy_m!=.)
gen gm_educ=(eduy_gm>3 & eduy_gm!=.)


* Two group variables

mat define edtest=J(12,4,.)
local m =1 

foreach vary of varlist  f_educ m_educ gm_educ   {
local j=1
foreach var of varlist eta teach_exp hvc_exp hvchild_exp{
if "`var'"=="eta"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat edtest[`m',`j']= r(mu_1) // var=0 no educ
	mat edtest[`m'+1,`j']= r(mu_2) //var=1  educ
	mat edtest[`m'+2,`j']=r(p)
	mat edtest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1

}
local m=`m'+4
}

mat list edtest

*Three group var : ability and age 
mat define abitest=J(12,4,.)
local m =1 

foreach vary of varlist  abi_lt_1_3 abi_lt_1_2 abi_lt_2_3 {
local j=1
foreach var of varlist eta teach_exp hvc_exp hvchild_exp{
if "`var'"=="eta"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat abitest[`m',`j']= r(mu_1) //lower abi
	mat abitest[`m'+1,`j']= r(mu_2) //high abi
	mat abitest[`m'+2,`j']=r(p)
	mat abitest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list abitest


mat define agetest=J(12,4,.)
local m =1 
foreach vary of varlist  age_g_1_3 age_g_1_2  age_g_2_3 {
	local j=1
foreach var of varlist eta teach_exp hvc_exp hvchild_exp{
if "`var'"=="eta"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat agetest[`m',`j']= r(mu_1) // Older Group
	mat agetest[`m'+1,`j']= r(mu_2) //Younger group
	mat agetest[`m'+2,`j']=r(p)
	mat agetest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list agetest




gen n=_n 
gen var=""
gen var2=""



preserve
	svmat edtest 
	rename (edtest1 edtest2 edtest3 edtest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Father's Educ<=9 Mean" if n== 1 
	replace var2="Father's Educ>9 Mean" if n== 2 
	replace var2="Mother's Educ<=9 Mean" if n== 5
	replace var2="Mother's Educ>9 Mean" if n== 6 
	replace var2="Grandmother's Educ<3 Mean" if n== 9 
	replace var2="Grandmother's Educ>=3 Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save edtest_l.dta, replace 
	texsave using "$datapath\edtest_l.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 



preserve
svmat agetest 
	rename (agetest1 agetest2 agetest3 agetest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)
	replace var2="Age enrollment 20-25 Months Mean" if n== 1 
	replace var2="Age enrollment 9-15 Months Mean" if n== 2 
	replace var2="Age enrollment 15-20 Months Mean" if n== 5
	replace var2="Age enrollment 9-15 Months Mean" if n== 6 
	replace var2="Age enrollment 20-25 Months Mean" if n== 9 
	replace var2="Age enrollment 15-20 Months Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save agetest_l.dta, replace 
	texsave using "$datapath\agetest_l.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 


preserve
	svmat abitest 
	rename (abitest1 abitest2 abitest3 abitest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Slow Mean" if n== 1 
	replace var2="Fast Mean" if n== 2 
	replace var2="Normal Mean" if n== 5
	replace var2="Fast Mean" if n== 6 
	replace var2="Slow Mean" if n== 9 
	replace var2="Normal Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save abitest_l.dta, replace 
	texsave using "$datapath\abitest_l.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 






**# Fine #2



/*
preserve
use "$datapath\estimates\maturation\fine_level_mul\point_estimates_mul_mat_wo.dta" , clear
keep if n>19 & n<27
sum m2 if n==20
gen teach=r(mean) 
sum m2 if n==21
gen hvc=r(mean) 
sum m2 if n==22
gen hvchild=r(mean)  
sum m2 if n==23
gen grandr=r(mean)  
gen id=1
keep if n ==20
keep id teach hvc hvchild grandr 
tempfile betas
save `betas'
restore 
*/

**# fine eta1
use eta_fine_mul_final_vec, clear
bys KIDID: egen first_a=max(first_agem)
replace eta1=. if eta1==0
collapse eta1 (max) first_a first_agem tabm_m intabm_m inttcm_m grandm male eduy_f eduy_m eduy_gm abi_ft (min) i, by (id ts_id)
cap drop _merge
// we can skip line 586 to line 595
merge m:1 id  using `betas'
cap drop temp
foreach var of varlist teach hvc hvchild grandr {
egen temp=max(`var')
replace `var'=temp
drop temp 
}
foreach var of varlist tabm_m intabm_m inttcm_m {
replace `var'=. if `var'==-99
}
// here you can manually general the variables using the point estimates from the excel. Here teach, hvc and hvchild indicates the point estimates of these three variables in the excel file.
gen teach_exp=exp(0.002*tabm_m) // exp(teach*tabm_m)

gen hvc_exp=exp(0.724*intabm_m) // exp(hvc*intabm_m)

gen hvchild_exp=exp(0.052*inttcm_m) // exp(hvchild*inttcm_m)


*Age of enrollment:
gen age_g=1 if first_a>=9 & first_a<15
replace  age_g=2 if first_a>=15 & first_a<=20
replace age_g=3 if first_a>20 & first_a<=25

gen age_g_1_2=(age_g==1 & age_g!=.) //9-15 vs 15-20
replace age_g_1_2=. if age_g==3 | age_g==.
gen age_g_1_3=(age_g==1 & age_g!=.) //9-15 vs 20-25
replace age_g_1_3=. if age_g==2 | age_g==.
gen age_g_2_3=(age_g==2 & age_g!=.) //15-20 vs 20-25
replace age_g_2_3=. if age_g==1 | age_g==.

* Ability 
gen abi_ft_1_2=(abi_ft==1 & abi_ft!=.) //fast-normal
replace abi_ft_1_2=. if abi_ft==3 | abi_ft==.
gen abi_ft_1_3=(abi_ft==1 & abi_ft!=.) //fast -slow
replace abi_ft_1_3=. if abi_ft==2 | abi_ft==.
gen abi_ft_2_3=(abi_ft==2 & abi_ft!=.) //normal-slow
replace abi_ft_2_3=. if abi_ft==1 | abi_ft==.


* Family Background 
gen f_educ=(eduy_f>9 & eduy_f!=.)
gen m_educ=(eduy_m>9 & eduy_m!=.)
gen gm_educ=(eduy_gm>3 & eduy_gm!=.)


* Two group variables

mat define edtest=J(12,4,.)
local m =1 

foreach vary of varlist  f_educ m_educ gm_educ   {
local j=1
foreach var of varlist eta1 teach_exp hvc_exp hvchild_exp{
if "`var'"=="eta1"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat edtest[`m',`j']= r(mu_1) // var=0 no educ
	mat edtest[`m'+1,`j']= r(mu_2) //var=1  educ
	mat edtest[`m'+2,`j']=r(p)
	mat edtest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list edtest

*Three group var : ability and age 
mat define abitest=J(12,4,.)
local m =1 

foreach vary of varlist  abi_ft_1_3 abi_ft_1_2 abi_ft_2_3 {
local j=1
foreach var of varlist eta1 teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta1"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat abitest[`m',`j']= r(mu_1) //lower abi
	mat abitest[`m'+1,`j']= r(mu_2) //high abi
	mat abitest[`m'+2,`j']=r(p)
	mat abitest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list abitest


mat define agetest=J(12,4,.)
local m =1 
foreach vary of varlist  age_g_1_3 age_g_1_2  age_g_2_3 {
	local j=1
foreach var of varlist eta1 teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta1"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat agetest[`m',`j']= r(mu_1) // Older Group
	mat agetest[`m'+1,`j']= r(mu_2) //Younger group
	mat agetest[`m'+2,`j']=r(p)
	mat agetest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list agetest




gen n=_n 
gen var=""
gen var2=""



preserve
	svmat edtest 
	rename (edtest1 edtest2 edtest3 edtest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Father's Educ<=9 Mean" if n== 1 
	replace var2="Father's Educ>9 Mean" if n== 2 
	replace var2="Mother's Educ<=9 Mean" if n== 5
	replace var2="Mother's Educ>9 Mean" if n== 6 
	replace var2="Grandmother's Educ<3 Mean" if n== 9 
	replace var2="Grandmother's Educ>=3 Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save edtest_f1.dta, replace 
	texsave using "$datapath\edtest_f1.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 



preserve
svmat agetest 
	rename (agetest1 agetest2 agetest3 agetest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)
	replace var2="Age enrollment 20-25 Months Mean" if n== 1 
	replace var2="Age enrollment 9-15 Months Mean" if n== 2 
	replace var2="Age enrollment 15-20 Months Mean" if n== 5
	replace var2="Age enrollment 9-15 Months Mean" if n== 6 
	replace var2="Age enrollment 20-25 Months Mean" if n== 9 
	replace var2="Age enrollment 15-20 Months Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save agetest_f1.dta, replace 
	texsave using "$datapath\agetest_f1.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 


preserve
	svmat abitest 
	rename (abitest1 abitest2 abitest3 abitest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Slow Mean" if n== 1 
	replace var2="Fast Mean" if n== 2 
	replace var2="Normal Mean" if n== 5
	replace var2="Fast Mean" if n== 6 
	replace var2="Slow Mean" if n== 9 
	replace var2="Normal Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save abitest_f1.dta, replace 
	texsave using "$datapath\abitest_f1.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 



**# fine eta2
use eta_fine_mul_final_vec, clear
bys KIDID: egen first_a=max(first_agem)
replace eta2=. if eta2==0
collapse eta2 (max) first_a first_agem tabm_m intabm_m inttcm_m grandm male eduy_f eduy_m eduy_gm abi_ft (min) i, by (id ts_id)
cap drop _merge

gen teach_exp=exp(0.002*tabm_m) // exp(teach*tabm_m)

gen hvc_exp=exp(0.724*intabm_m) // exp(hvc*intabm_m)

gen hvchild_exp=exp(0.052*inttcm_m) // exp(hvchild*inttcm_m)

*Age of enrollment:
gen age_g=1 if first_a>=9 & first_a<15
replace  age_g=2 if first_a>=15 & first_a<=20
replace age_g=3 if first_a>20 & first_a<=25

gen age_g_1_2=(age_g==1 & age_g!=.) //9-15 vs 15-20
replace age_g_1_2=. if age_g==3 | age_g==.
gen age_g_1_3=(age_g==1 & age_g!=.) //9-15 vs 20-25
replace age_g_1_3=. if age_g==2 | age_g==.
gen age_g_2_3=(age_g==2 & age_g!=.) //15-20 vs 20-25
replace age_g_2_3=. if age_g==1 | age_g==.

* Ability 
gen abi_ft_1_2=(abi_ft==1 & abi_ft!=.) //fast-normal
replace abi_ft_1_2=. if abi_ft==3 | abi_ft==.
gen abi_ft_1_3=(abi_ft==1 & abi_ft!=.) //fast -slow
replace abi_ft_1_3=. if abi_ft==2 | abi_ft==.
gen abi_ft_2_3=(abi_ft==2 & abi_ft!=.) //normal-slow
replace abi_ft_2_3=. if abi_ft==1 | abi_ft==.


* Family Background 
gen f_educ=(eduy_f>9 & eduy_f!=.)
gen m_educ=(eduy_m>9 & eduy_m!=.)
gen gm_educ=(eduy_gm>3 & eduy_gm!=.)


* Two group variables

mat define edtest=J(12,4,.)
local m =1 

foreach vary of varlist  f_educ m_educ gm_educ   {
local j=1
foreach var of varlist eta2 teach_exp hvc_exp hvchild_exp{
if "`var'"=="eta2"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat edtest[`m',`j']= r(mu_1) // var=0 no educ
	mat edtest[`m'+1,`j']= r(mu_2) //var=1  educ
	mat edtest[`m'+2,`j']=r(p)
	mat edtest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list edtest

*Three group var : ability and age 
mat define abitest=J(12,4,.)
local m =1 

foreach vary of varlist  abi_ft_1_3 abi_ft_1_2 abi_ft_2_3 {
local j=1
foreach var of varlist eta2 teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta2"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat abitest[`m',`j']= r(mu_1) //lower abi
	mat abitest[`m'+1,`j']= r(mu_2) //high abi
	mat abitest[`m'+2,`j']=r(p)
	mat abitest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list abitest


mat define agetest=J(12,4,.)
local m =1 
foreach vary of varlist  age_g_1_3 age_g_1_2  age_g_2_3 {
	local j=1
foreach var of varlist eta2 teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta2"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat agetest[`m',`j']= r(mu_1) // Older Group
	mat agetest[`m'+1,`j']= r(mu_2) //Younger group
	mat agetest[`m'+2,`j']=r(p)
	mat agetest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list agetest


gen n=_n 
gen var=""
gen var2=""



preserve
	svmat edtest 
	rename (edtest1 edtest2 edtest3 edtest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Father's Educ<=9 Mean" if n== 1 
	replace var2="Father's Educ>9 Mean" if n== 2 
	replace var2="Mother's Educ<=9 Mean" if n== 5
	replace var2="Mother's Educ>9 Mean" if n== 6 
	replace var2="Grandmother's Educ<3 Mean" if n== 9 
	replace var2="Grandmother's Educ>=3 Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save edtest_f2.dta, replace 
	texsave using "$datapath\edtest_f2.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 



preserve
svmat agetest 
	rename (agetest1 agetest2 agetest3 agetest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)
	replace var2="Age enrollment 20-25 Months Mean" if n== 1 
	replace var2="Age enrollment 9-15 Months Mean" if n== 2 
	replace var2="Age enrollment 15-20 Months Mean" if n== 5
	replace var2="Age enrollment 9-15 Months Mean" if n== 6 
	replace var2="Age enrollment 20-25 Months Mean" if n== 9 
	replace var2="Age enrollment 15-20 Months Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save agetest_f2.dta, replace 
	texsave using "$datapath\agetest_f2.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 


preserve
	svmat abitest 
	rename (abitest1 abitest2 abitest3 abitest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Slow Mean" if n== 1 
	replace var2="Fast Mean" if n== 2 
	replace var2="Normal Mean" if n== 5
	replace var2="Fast Mean" if n== 6 
	replace var2="Slow Mean" if n== 9 
	replace var2="Normal Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save abitest_f2.dta, replace 
	texsave using "$datapath\abitest_f2.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 





**# fine eta3

use eta_fine_mul_final_vec, clear
bys KIDID: egen first_a=max(first_agem)
replace eta3=. if eta3==0
collapse eta3 (max) first_a first_agem tabm_m intabm_m inttcm_m grandm male eduy_f eduy_m eduy_gm abi_ft (min) i, by (id ts_id)
cap drop _merge

gen teach_exp=exp(0.002*tabm_m) // exp(teach*tabm_m)

gen hvc_exp=exp(0.724*intabm_m) // exp(hvc*intabm_m)

gen hvchild_exp=exp(0.052*inttcm_m) // exp(hvchild*inttcm_m)

*Age of enrollment:
gen age_g=1 if first_a>=9 & first_a<15
replace  age_g=2 if first_a>=15 & first_a<=20
replace age_g=3 if first_a>20 & first_a<=25

gen age_g_1_2=(age_g==1 & age_g!=.) //9-15 vs 15-20
replace age_g_1_2=. if age_g==3 | age_g==.
gen age_g_1_3=(age_g==1 & age_g!=.) //9-15 vs 20-25
replace age_g_1_3=. if age_g==2 | age_g==.
gen age_g_2_3=(age_g==2 & age_g!=.) //15-20 vs 20-25
replace age_g_2_3=. if age_g==1 | age_g==.

* Ability 
gen abi_ft_1_2=(abi_ft==1 & abi_ft!=.) //fast-normal
replace abi_ft_1_2=. if abi_ft==3 | abi_ft==.
gen abi_ft_1_3=(abi_ft==1 & abi_ft!=.) //fast -slow
replace abi_ft_1_3=. if abi_ft==2 | abi_ft==.
gen abi_ft_2_3=(abi_ft==2 & abi_ft!=.) //normal-slow
replace abi_ft_2_3=. if abi_ft==1 | abi_ft==.


* Family Background 
gen f_educ=(eduy_f>9 & eduy_f!=.)
gen m_educ=(eduy_m>9 & eduy_m!=.)
gen gm_educ=(eduy_gm>3 & eduy_gm!=.)


* Two group variables

mat define edtest=J(12,4,.)
local m =1 

foreach vary of varlist  f_educ m_educ gm_educ   {
local j=1
foreach var of varlist eta3 teach_exp hvc_exp hvchild_exp{
if "`var'"=="eta3"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat edtest[`m',`j']= r(mu_1) // var=0 no educ
	mat edtest[`m'+1,`j']= r(mu_2) //var=1  educ
	mat edtest[`m'+2,`j']=r(p)
	mat edtest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list edtest

*Three group var : ability and age 
mat define abitest=J(12,4,.)
local m =1 

foreach vary of varlist  abi_ft_1_3 abi_ft_1_2 abi_ft_2_3 {
local j=1
foreach var of varlist eta3 teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta3"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat abitest[`m',`j']= r(mu_1) //lower abi
	mat abitest[`m'+1,`j']= r(mu_2) //high abi
	mat abitest[`m'+2,`j']=r(p)
	mat abitest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list abitest


mat define agetest=J(12,4,.)
local m =1 
foreach vary of varlist  age_g_1_3 age_g_1_2  age_g_2_3 {
	local j=1
foreach var of varlist eta3 teach_exp hvc_exp hvchild_exp{

if "`var'"=="eta3"{
ttest `var' if `var'>0 , by(`vary')	
}
else {
ttest `var' , by(`vary')
}
	mat agetest[`m',`j']= r(mu_1) // Older Group
	mat agetest[`m'+1,`j']= r(mu_2) //Younger group
	mat agetest[`m'+2,`j']=r(p)
	mat agetest[`m'+3,`j']=r(N_2)+r(N_1)
local j=`j'+1
}
local m=`m'+4
}

mat list agetest


gen n=_n 
gen var=""
gen var2=""



preserve
	svmat edtest 
	rename (edtest1 edtest2 edtest3 edtest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Father's Educ<=9 Mean" if n== 1 
	replace var2="Father's Educ>9 Mean" if n== 2 
	replace var2="Mother's Educ<=9 Mean" if n== 5
	replace var2="Mother's Educ>9 Mean" if n== 6 
	replace var2="Grandmother's Educ<3 Mean" if n== 9 
	replace var2="Grandmother's Educ>=3 Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save edtest_f3.dta, replace 
	texsave using "$datapath\edtest_f3.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 



preserve
svmat agetest 
	rename (agetest1 agetest2 agetest3 agetest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)
	replace var2="Age enrollment 20-25 Months Mean" if n== 1 
	replace var2="Age enrollment 9-15 Months Mean" if n== 2 
	replace var2="Age enrollment 15-20 Months Mean" if n== 5
	replace var2="Age enrollment 9-15 Months Mean" if n== 6 
	replace var2="Age enrollment 20-25 Months Mean" if n== 9 
	replace var2="Age enrollment 15-20 Months Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save agetest_f3.dta, replace 
	texsave using "$datapath\agetest_f3.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 


preserve
	svmat abitest 
	rename (abitest1 abitest2 abitest3 abitest4) (Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child)

	replace var2="Slow Mean" if n== 1 
	replace var2="Fast Mean" if n== 2 
	replace var2="Normal Mean" if n== 5
	replace var2="Fast Mean" if n== 6 
	replace var2="Slow Mean" if n== 9 
	replace var2="Normal Mean" if n== 10
	replace var2="P-value" if n==3 | n==7 | n==11  
	replace var2="N" if n==4 | n==8 | n==12  
	keep if n<13
	keep var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child n
	order var var2 Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child
	gen s_Eta=1 if Eta <0.1 & (n==3 | n==7 | n==11)
	gen s_Teaching_ability=1 if Teaching_ability <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Caregiver=1 if Home_Visitor_Caregiver <0.1 & (n==3 | n==7 | n==11 )
	gen s_Home_Visitor_Child=1 if Home_Visitor_Child <0.1 & (n==3 | n==7 | n==11 )

	tostring  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child , replace force format(%6.3f) 
	foreach var in  Eta Teaching_ability Home_Visitor_Caregiver Home_Visitor_Child {
	replace `var' = "(" + `var' + ")" if  (n==3 | n==7 | n==11 )
	replace `var' = "\textbf{" + `var' + "}" if s_`var'==1 & (n==3 | n==7 | n==11 )

	}
	drop n s_* var


	save abitest_f3.dta, replace 
	texsave using "$datapath\abitest_f3.tex", width(\raggedleft) ///
		replace size(scriptsize) location(ht) varlabels frag ///
		title(Test) marker(tab:`s'abi_denv)  ///
		footnote("Note:", size(scriptsize)) autonumber 
restore 







replace var="Grandmother Rearing" if n==2 
*replace var="Teaching_ability" if n==5 
*replace var="Interaction quality: Home Visitor and Caregiver" if n==9 
*replace var="Interaction quality: Home Visitor and Child" if n==13


/*For sum table 
*use "$datapath\task_learning_fortran_0526.dta" , clear
cap drop _merge
merge 1:1 id ts_id using "eta_maturation_cog_mul"
to ETA data set.
*OJO CON LAS TRANFORMACIONES!
the results:  eta*interaction

*Luego merge abi measures
foreach vary of varlist  f_educ m_educ gm_educ  {
foreach var of varlist eta teach_exp hvc_exp hvchild_exp{
ttest `var' , by(`vary')
	gen p_`vary'_`var'=r(p)
	mat n_`vary'_`var'=r(N_2)+r(N_1)
}
	
}

foreach var of varlist f_educ m_educ gm_educ  {
preserve 
collapse (mean) eta teach_exp hvc_exp hvchild_exp  (max) p_`var'_eta- p_`var'_hvchild_exp n_`var'_eta- n_`var'_hvchild_exp, by(`var')
save "$datapath\Alejandra\Summary\sum_c_`var'.dta", replace 

restore
	
}